import static org.junit.Assert.assertEquals;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;

import org.junit.Test;


public class DateTest {
	/**
	 * Extract month, day and year int values
	 * from the private fields of Date d
	 * @param d, the Date object
	 * @return an array of size 3 with values of
	 * month, day and year at corresponding index
	 */
	private int[] extractFieldValue(Date date) {
		Month mon = NextDayTest.getFieldValue(date, "m");
		Day day = NextDayTest.getFieldValue(date, "d");
		Year year = NextDayTest.getFieldValue(date, "y");
		
		int[] fields = new int[3];
		fields[0] = mon.getMonth();
		fields[1] = day.getDay();
		fields[2] = year.getYear();
		return fields;
	}

	@Test public void incrementTest() {
		Date d = new Date(12, 17, 2011);
		d.increment();
		int[] fields = extractFieldValue(d);
		assertEquals(12, fields[0]);
		assertEquals(18, fields[1]);
		assertEquals(2011, fields[2]);
	}
	
	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 1
	 * Purpose: test normal increment
	 * Priority: 2
	 */
	@Test public void incrementTest1() {
	    Date d = new Date(12, 17, 2011);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(12, fields[0]);
	    assertEquals(18, fields[1]);
	    assertEquals(2011, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 2
	 * Purpose: test normal increment
	 * Priority: 2
	 */
	@Test public void incrementTest2() {
	    Date d = new Date(5, 12, 2008);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(5, fields[0]);
	    assertEquals(13, fields[1]);
	    assertEquals(2008, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 3
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest3() {
	    Date d = new Date(1, 31, 2005);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(2, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2005, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 4
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest4() {
	    Date d = new Date(2, 28, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(3, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 5
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest5() {
	    Date d = new Date(3, 31, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(4, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 6
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest6() {
	    Date d = new Date(4, 30, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(5, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 7
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest7() {
	    Date d = new Date(5, 31, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(6, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 8
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest8() {
	    Date d = new Date(6, 30, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(7, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 9
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest9() {
	    Date d = new Date(7, 31, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(8, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 10
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest10() {
	    Date d = new Date(8, 31, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(9, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 11
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest11() {
	    Date d = new Date(9, 30, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(10, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 12
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest12() {
	    Date d = new Date(10, 31, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(11, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 13
	 * Purpose: test month increment
	 * Priority: 2
	 */
	@Test public void incrementTest13() {
	    Date d = new Date(11, 30, 2002);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(12, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2002, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 14
	 * Purpose: test year increment
	 * Priority: 2
	 */
	@Test public void incrementTest14() {
	    Date d = new Date(12, 31, 1999);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(1, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(2000, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 15
	 * Purpose: test year increment
	 * Priority: 2
	 */
	@Test public void incrementTest15() {
	    Date d = new Date(12, 31, 1995);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(1, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(1996, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 16
	 * Purpose: test leap year
	 * Priority: 1
	 */
	@Test public void incrementTest16() {
	    Date d = new Date(2, 28, 1992);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(2, fields[0]);
	    assertEquals(29, fields[1]);
	    assertEquals(1992, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 17
	 * Purpose: test leap year
	 * Priority: 1
	 */
	@Test public void incrementTest17() {
	    Date d = new Date(2, 29, 1988);
	    d.increment();
	    int[] fields = extractFieldValue(d);
	    assertEquals(3, fields[0]);
	    assertEquals(1, fields[1]);
	    assertEquals(1988, fields[2]);
	}

	/**
	 * @author: kavinyao
	 * @date: 2011-12-19T17:19:44.058000
	 * @version: 0.1
	 * @TCID: 18
	 * Purpose: test date print
	 * Priority: 1
	 */
	@Test public void printDateTest1() {
		PrintStream sysout = System.out; //backup
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		System.setOut(new PrintStream(baos));
		Date date = new Date(12, 18, 2011);
		date.increment();
		date.printDate();
		assertEquals("12/19/2011" + System.getProperty("line.separator"), baos.toString());
		System.setOut(sysout);//restore original System.out
	}
}
